function initiate_geolocation() { 
	// if(sessionID_ == 0){
		// var d = new Date();
		// sessionID_ = name_+"-"+d.getTime();
	// }
	if(watchPositionId == null){			
		watchPositionId = navigator.geolocation.watchPosition(handle_geolocation_query, handle_errors, {enableHighAccuracy:true, maximumAge:60000, timeout:30000});			
	}		
	if(story_ == null){
		$.get('data/story.txt', function(data){
			story_ = data;
		});
	}

}  
   
function handle_errors(error){  
    switch(error.code){  
        case error.PERMISSION_DENIED: alert("User did not share geolocation data");  
        break;  
  
        case error.POSITION_UNAVAILABLE: alert("Could not detect current position");  
        break;  
  
        case error.TIMEOUT: alert("Retrieving position timed out");  
        break;  
  
        default: alert("Error while retrieving the geolocation");  
        break;  
    }  
}  
  
function handle_geolocation_query(position){ 
	counter_ = 0;
	accuracy_ = position.coords.accuracy;
	speed_ = position.coords.speed;
	poiReady = false;
	calculateArea(position.coords.latitude, position.coords.longitude);
	coordToAddress(position.coords.latitude, position.coords.longitude);
	lat_ = position.coords.latitude;
	lon_ = position.coords.longitude;
	getPOI('cafe', "" + lat_ + "," + lon_);
    getPOI('pub', "" + lat_ + "," + lon_);
    getPOI('station', "" + lat_ + "," + lon_);
    getPOI('bakery', "" + lat_ + "," + lon_);

	
	
} 


function coordToAddress(lat, lon){
	var geocoder = new GClientGeocoder();
	var point = new GLatLng(lat, lon);
	geocoder.getLocations(point, getLocationInformation);
}

function getLocationInformation(response)
{	
	setAddress(response.Placemark[0].address);
}

function setAddress(addr){			
	var addr_ = addr.split(",");
	oldStreet_ = street_;
	street_ = addr_[0];
	city_ = addr_[1].split(" ")[2];
	country_ = addr_[2];
	$('#footer').html("<h5>Aktuelle Position: "+street_+"</h5>");
	getPositionWeather(city_);		
}

var getPOI =function(where, around){

	$.ajaxSetup({
		  "error":function() {console.log("poi error");} //alert("Couldn't retrieve POI's");  }
	});


	$.getJSON("geosearchProxy.php?where="+where+"&around="+around, function(data){
		//alert(data.features[0].properties.name);
		if(where.indexOf("pub")>=0) pub_ = data.features[0];//data.features[0].properties.name;
		if(where.indexOf("bakery")>=0) bakery_ = data.features[0];
		if(where.indexOf("cafe")>=0) cafe_ = data.features[0];
		if(where.indexOf("station")>=0) {
			trainStation_ = data.features[0];
			if(trainStation_.properties.name.indexOf("Hbf") >=0){
				trainStation_.properties.name = trainStation_.properties.name.replace("Hbf", "Hauptbahnhof");
			}
			if(trainStation_.properties.name.indexOf("Bf") >=0){
				trainStation_.properties.name = trainStation_.properties.name.replace("Bf", "Bahnhof");
			}
		}
		
		counter_++;
		if(counter_ == 4){
		
			poiReady = true;
			if(poiWait){
				startStory();
				poiWait = false;
			}
			
		}
	});
}

//Weather
function Weather(condition, tempC, humidity){
	this.condition = condition;
	this.tempC = tempC;
	this.humidity = humidity;
}

var childData	= function(selector, arg)
 {
 	return selector.find(arg).attr('data');
 }
var getPositionWeather =function(where){

	where = where.replace('ä','ae');
	where = where.replace('ö','oe');
	where = where.replace('ü','ue');
	where = where.replace('ß','ss');
	
	

	where = 'muenster';
	
	//$('#data, #error').stop().hide('fast');
	$.ajax({
	   type: "GET",
	   data:"where="+where,
	   url: "weatherProxy.php",
		success: function(data){

			forecast	= $(data).find('forecast_information');
			cCondition	= $(data).find('current_conditions');

			city	= childData(forecast, 'city');
			if(city!=undefined)
			{
				date	= childData(forecast, 'forecast_date');
			
				condition	= childData(cCondition, 'condition');
				tempC	= childData(cCondition, 'temp_c');
				humidity	= childData(cCondition, 'humidity');
				icon	= childData(cCondition, 'icon');
				
				weather_ = new Weather(condition, tempC, humidity);

			}
			else
			{
				alert('Wetterdaten konnten nicht abgerufen werden');
			}

		}

	 });
}

	
function calculateArea(areaLat, areaLon){
	// 0=leftUpperY, 1=leftUpperX, 2=rightUpperY, 3=rightUppreX, 4=leftBottmY, 5=leftBottomX, 6=rightBottomY, 7=rightBottomX
	var area1 = new Array(51.973038,7.596331,51.973038,7.640619,51.962039,7.596331,51.962039,7.640619);
	var area2 = new Array(51.962039,7.596331,51.962039,7.640619,51.947863,7.596331,51.947863,7.640619);
	var areaLine = 51.962039;
	
	//Area 1 or 4
	if(areaLat > areaLine){
		if((areaLat < area1[0]) && (areaLon > area1[1]) && (areaLon < area1[3])){
			area_ = 1;
		}
		else{
			area_ = 4;
		}
	}
	else{
		if((areaLat > area2[6]) && (areaLon > area2[1]) && (areaLon < area2[3])){
			area_  = 2;
		}
		else{
			area_ = 3;
		}
	}

}